<template>
  <div class="parking-assistant">
    <!-- 对话框容器 -->
    <div class="chat-frame">
      <!-- 沉浸式标题栏 -->
      <header class="chat-header">
        <div class="header-content">
          <div class="title-group">
            <div class="logo-badge">
              <svg class="parking-icon" viewBox="0 0 24 24">
                <path fill="#4361ee" d="M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2m4.2 12v2h1.5v-2h.8l1.2-1.2V9.2L11.5 8H9.2v7h1.5m1.5-3.6c1 0 1.8.8 1.8 1.8s-.8 1.8-1.8 1.8-1.8-.8-1.8-1.8.8-1.8 1.8-1.8z"/>
              </svg>
            </div>
            <div class="title-wrapper">
              <h1>清泓智巡AI助手</h1>
              <div class="status">在线服务中</div>
            </div>
          </div>
        </div>
      </header>

      <!-- 消息视图 -->
      <main ref="messageContainer" class="message-viewport">
        <transition-group 
          name="message-stack"
          tag="div"
          class="messages-flow"
        >
          <!-- 用户消息 -->
          <div 
            v-for="(msg, index) in messages"
            :key="index"
            :class="['message-bubble', msg.role]"
          >
            <div v-if="msg.role === 'user'" class="user-message">
              <div class="bubble user">
                <div class="content">
                  <div class="text">{{ msg.content }}</div>
                  <div class="meta">
                    <span class="time">{{ formatTime(msg.timestamp) }}</span>
                    <div class="status-indicator">
                      <svg class="check-icon" viewBox="0 0 16 16">
                        <path fill="currentColor" d="M13.48 3.43a.75.75 0 0 1 1.06 1.06l-7 7a.75.75 0 0 1-1.06 0l-3-3a.75.75 0 1 1 1.06-1.06l2.47 2.47 6.47-6.47z"/>
                      </svg>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- AI回复 -->
            <div v-else class="ai-message">
              <div class="bubble ai">
                <div class="ai-header">
                  <div class="ai-avatar">
                    <svg class="ai-icon" viewBox="0 0 24 24">
                      <path fill="#4361ee" d="M21 9c-1.1 0-2 .9-2 2v4c0 1.1.9 2 2 2h1v2H2v-2h1c1.1 0 2-.9 2-2v-4c0-1.1-.9-2-2-2H2V7h20v2h-1M8 11H4v4h4v-4m6 0h-4v4h4v-4m6 0h-4v4h4v-4z"/>
                    </svg>
                  </div>
                  <div class="ai-title">AI助手</div>
                </div>
                <div class="content">
                  <div class="text">{{ msg.content }}</div>
                  <div class="meta">
                    <span class="time">{{ formatTime(msg.timestamp) }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition-group>
      </main>

      <!-- 专业输入区 -->
      <footer class="input-area">
        <div class="input-container">
          <div class="input-wrapper">
            <input
              v-model="userInput"
              placeholder="输入船泊相关问题..."
              @keyup.enter="sendMessage"
              :disabled="isLoading"
            />
            <div class="action-group">
              <button class="func-btn">
                <svg class="attach-icon" viewBox="0 0 24 24">
                  <path fill="#64748b" d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5a2.5 2.5 0 0 1 5 0v10.5c0 .55-.45 1-1 1s-1-.45-1-1V6H10v9.5a2.5 2.5 0 0 0 5 0V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z"/>
                </svg>
              </button>
              <button 
                class="send-btn"
                @click="sendMessage"
                :disabled="isLoading"
              >
                <svg v-if="!isLoading" class="send-icon" viewBox="0 0 24 24">
                  <path fill="currentColor" d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/>
                </svg>
                <svg v-else class="loading-icon" viewBox="0 0 24 24">
                  <path fill="currentColor" d="M12 4V2A10 10 0 0 0 2 12h2a8 8 0 0 1 8-8z"/>
                </svg>
              </button>
            </div>
          </div>
        </div>
      </footer>
    </div>
  </div>
</template>

<script setup>
/* 保持原有逻辑不变 */
import { ref, nextTick } from 'vue'
import axios from 'axios'

const apiKey = 'sk-90b59c6ae12343aabe252f557d2a3dc1'
const messages = ref([])
const userInput = ref('')
const isLoading = ref(false)
const messageContainer = ref(null)

const formatTime = timestamp => {
  return new Date(timestamp).toLocaleTimeString('zh-CN', {
    hour: '2-digit',
    minute: '2-digit'
  })
}

const sendMessage = async () => {
  if (!userInput.value.trim() || isLoading.value) return
  
  const content = userInput.value
  messages.value.push({
    role: 'user',
    content,
    timestamp: Date.now()
  })
  userInput.value = ''
  isLoading.value = true

  try {
    const { data } = await axios.post('https://api.deepseek.com/v1/chat/completions', {
      model: 'deepseek-chat',
      messages: [{ role: 'user', content }]
    }, {
      headers: {
        Authorization: `Bearer ${apiKey}`,
        'Content-Type': 'application/json'
      }
    })

    messages.value.push({
      role: 'bot',
      content: data.choices[0].message.content,
      timestamp: Date.now()
    })
  } catch (error) {
    console.error(error)
    messages.value.push({
      role: 'bot',
      content: '请求失败，请稍后再试',
      timestamp: Date.now()
    })
  } finally {
    isLoading.value = false
    nextTick(() => {
      if (messageContainer.value) {
        messageContainer.value.scrollTo({
          top: messageContainer.value.scrollHeight,
          behavior: 'smooth'
        })
      }
    })
  }
}
</script>

<style scoped>
.parking-assistant {
  --primary: #4361ee;
  --primary-light: #eef2ff;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  height: 83vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f8fafc;
}

/* 专业对话框容器 */
.chat-frame {
  width: 80vw;
  max-width: 720px;
  height: 82vh;
  background: white;
  border-radius: 20px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 沉浸式标题栏 */
.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--primary-light);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
}

.title-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-badge {
  width: 40px;
  height: 40px;
  background: white;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 6px -1px rgba(67, 97, 238, 0.1);
}

.parking-icon {
  width: 24px;
  height: 24px;
}

.title-wrapper h1 {
  font-size: 18px;
  color: var(--text-primary);
  font-weight: 600;
  margin: 0;
}

.status {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* 消息视窗 */
.message-viewport {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background: #f8f9fc;
}

.messages-flow {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 消息气泡系统 */
.message-bubble {
  display: flex;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-message {
  margin-left: auto;
  max-width: 75%;
}

.ai-message {
  margin-right: auto;
  max-width: 75%;
}

/* 用户消息样式 */
.bubble.user {
  background: var(--primary);
  color: white;
  border-radius: 18px 4px 18px 18px;
  padding: 14px 18px;
  position: relative;
  box-shadow: 0 2px 8px rgba(67, 97, 238, 0.15);
}

/* AI消息样式 */
.bubble.ai {
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px 18px 18px 18px;
  padding: 18px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.ai-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.ai-avatar {
  width: 28px;
  height: 28px;
  background: var(--primary-light);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

.text {
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
}

.bubble.user .text {
  color: white;
}

.meta {
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.time {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
}

.bubble.ai .time {
  color: var(--text-secondary);
}

.status-indicator {
  display: flex;
  align-items: center;
}

.check-icon {
  width: 14px;
  height: 14px;
  margin-left: 8px;
  opacity: 0.8;
}

/* 专业输入区 */
.input-area {
  border-top: 1px solid var(--border);
  padding: 16px 24px;
  background: white;
}

.input-wrapper {
  display: flex;
  gap: 8px;
  background: white;
  border-radius: 16px;
  border: 1px solid var(--border);
  padding: 8px;
}

input {
  flex: 1;
  border: none;
  padding: 12px 16px;
  font-size: 15px;
  background: transparent;
}

input::placeholder {
  color: #94a3b8;
}

.action-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.func-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  transition: background 0.2s;
}

.func-btn:hover {
  background: #f1f5f9;
}

.send-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: var(--primary);
  color: white;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s;
}

.send-btn:hover {
  opacity: 0.9;
}

.send-icon, .loading-icon {
  width: 20px;
  height: 20px;
}

/* 动画效果 */
.message-stack-enter-active {
  transition: all 0.3s ease;
}

.message-stack-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 768px) {
  .chat-frame {
    height: 100vh;
    border-radius: 0;
  }
  
  .message-viewport {
    padding: 16px;
  }
  
  .bubble {
    max-width: 85%;
  }
}
</style>